﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string subject_Add(subject model ,int ACID);


[OperationContract]
string subject_Update(subject model ,int ACID);


[OperationContract]
string subjectBysubid_Delete(int subid,int ACID);


[OperationContract]
subject subjectBysubid_Get(int subid,int ACID );






}
public partial class Service : IService
{
#region  添加subject表数据
/// <summary>
/// 添加subject
/// </summary>
/// <param name="model">subject实体</param>
/// <returns>空或错误</returns>
public string subject_Add(subject model,int ACID)
{
if(model.subid==null )
{
return "『会计科目ID』不能为空！";
}
if(model.hscash==null )
{
return "『核算现金流量标志』不能为空！";
}
if(model.detailflag==null )
{
return "『明细标志』不能为空！";
}
if(model.dcflag==null )
{
return "『借贷标志』不能为空！";
}
if(string.IsNullOrEmpty(model.name))
{
return "『科目名称』不能为空！";
}
if(string.IsNullOrEmpty(model.subcode))
{
return "『会计科目编码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.subject.Count(p=>p.subid==model.subid)>0)
{
return "主键已存在！";
}
ctx.subject.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加subject表数据事物用重构
/// <summary>
/// 添加subject事物用重构
/// </summary>
/// <param name="model">subject实体</param>
/// <returns>空或错误</returns>
private string subject_Add(subject model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.subid==null )
{
return "『会计科目ID』不能为空！";
}
if(model.hscash==null )
{
return "『核算现金流量标志』不能为空！";
}
if(model.detailflag==null )
{
return "『明细标志』不能为空！";
}
if(model.dcflag==null )
{
return "『借贷标志』不能为空！";
}
if(string.IsNullOrEmpty(model.name))
{
return "『科目名称』不能为空！";
}
if(string.IsNullOrEmpty(model.subcode))
{
return "『会计科目编码』不能为空！";
}
if(ctx.subject.Count(p=>p.subid==model.subid)>0)
{
return "主键已存在！";
}
ctx.subject.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改subject表数据
/// <summary>
/// 修改subject
/// </summary>
/// <param name="model">subject实体</param>
/// <returns>空或错误</returns>
public string subject_Update(subject model,int ACID)
{
if(model.subid==null )
{
return "『会计科目ID』不能为空！";
}
if(model.hscash==null )
{
return "『核算现金流量标志』不能为空！";
}
if(model.detailflag==null )
{
return "『明细标志』不能为空！";
}
if(model.dcflag==null )
{
return "『借贷标志』不能为空！";
}
if(string.IsNullOrEmpty(model.name))
{
return "『科目名称』不能为空！";
}
if(string.IsNullOrEmpty(model.subcode))
{
return "『会计科目编码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.subject.Count(p=>p.subid==model.subid)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
subject subjectmodel=ctx.subject.Single(p => p.subid==model.subid);
if(model.subid!=null )
{
subjectmodel.subid=model.subid;
}
if(!string.IsNullOrEmpty(model.subcode))
{
subjectmodel.subcode=model.subcode;
}
if(!string.IsNullOrEmpty(model.name))
{
subjectmodel.name=model.name;
}
if(model.dcflag!=null )
{
subjectmodel.dcflag=model.dcflag;
}
if(model.detailflag!=null )
{
subjectmodel.detailflag=model.detailflag;
}
if(model.hscash!=null )
{
subjectmodel.hscash=model.hscash;
}
if(model.hswbflag!=null )
{
subjectmodel.hswbflag=model.hswbflag;
}
if(model.attrflag!=null )
{
subjectmodel.attrflag=model.attrflag;
}
if(!string.IsNullOrEmpty(model.helpcode))
{
subjectmodel.helpcode=model.helpcode;
}
if(!string.IsNullOrEmpty(model.specialcode))
{
subjectmodel.specialcode=model.specialcode;
}
if(!string.IsNullOrEmpty(model.fullname))
{
subjectmodel.fullname=model.fullname;
}
if(model.relevantid!=null )
{
subjectmodel.relevantid=model.relevantid;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除subject表Bysubid数据
/// <summary>
/// 删除subjectBysubid
/// </summary>
/// <param name="subid">会计科目ID</param>
/// <returns>空或错误</returns>
public string subjectBysubid_Delete(int subid,int ACID)
{
if(subid==null )
{
return "『会计科目ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.subject.Count(p=>p.subid==subid)!=1)
{
return null;
}
subject model=ctx.subject.Single(p => p.subid== subid);
ctx.subject.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除subject表Bysubid数据[事物用重构]
/// <summary>
/// 删除subjectBysubid[事物用重构]
/// </summary>
/// <param name="subid">会计科目ID</param>
/// <returns>空或错误</returns>
private string subjectBysubid_Delete(int subid,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(subid==null )
{
return "『会计科目ID』不能为空！";
}
if(ctx.subject.Count(p=>p.subid==subid)!=1)
{
return null;
}
subject model=ctx.subject.Single(p => p.subid== subid);
ctx.subject.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取subject表Bysubid数据
/// <summary>
/// 获取subjectBysubid
/// </summary>
/// <param name="subid">会计科目ID</param>
/// <returns>当前表</returns>
public subject subjectBysubid_Get(int subid,int ACID)
{
if(subid==null )
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.subject.Count(p=>p.subid==subid)!=1)
{
return null;
}
return ctx.subject.SingleOrDefault(p=>p.subid==subid);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取subject表数据
/// <summary>
/// 获取subject
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<subject></returns>
public List<subject> subject_Get(Expression<Func<subject, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.subject.ToList();
}
 else 
{
return ctx.subject.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.subject.Where(predicate).ToList();
}
 else 
{
return ctx.subject.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion




#region  获取subject表数据计数
/// <summary>
/// 获取subject表数据计数
/// </summary>
/// <returns>List<subject></returns>
public int subject_GetCount(Expression<Func<subject, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.subject.Count();

}
 else
{
return ctx.subject.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取subject分页表数据
/// <summary>
/// 获取subject分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<subject></returns>
public List<subject> subject_Getpage(int pageIndex, int pageSize,Expression<Func<subject, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.subject.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.subject.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.subject.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.subject.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改subject表数据
/// <summary>
/// 批量修改subject
/// </summary>
/// <param name="predicate">Expression<Func<subject, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<subject,subject>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int subject_batchUpdate(Expression<Func<subject, bool>> predicate,Expression<Func<subject, subject>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.subject.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改subject表数据事物用重构
/// <summary>
/// 批量修改subject事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<subject, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<subject,subject>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int subject_batchUpdate(Expression<Func<subject, bool>> predicate,Expression<Func<subject, subject>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.subject.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除subject表数据
/// <summary>
/// 批量删除subject
/// </summary>
/// <param name="predicate">Expression<Func<subject, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int subject_batchDel(Expression<Func<subject, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.subject.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除subject表数据事物用重构
/// <summary>
/// 批量删除subject事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<subject, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int subject_batchDel(Expression<Func<subject, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.subject.Delete(predicate);
return ok;
}
#endregion


}
}

